package com.hlkj.warboot.module_sys.entity.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hlkj.warboot.configurer.util.StrUtil;
import com.hlkj.warboot.module_sys.entity.AdminEntity;


/**
 * @Description: 动态生成QueryWrapper<AdminEntity>对象
 * @Author: lixing
 * @Date: 2020/6/12 17:27
 */
public class AdminWrapper {
    public static QueryWrapper<AdminEntity> queryWrapper(AdminEntity entity) {
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("grade").orderByAsc("create_time");
        if (entity.baseCreateTimeStart != null) {
            queryWrapper.ge("create_time", entity.baseCreateTimeStart);
        }
        if (entity.baseCreateTimeEnd != null) {
            queryWrapper.le("create_time", entity.baseCreateTimeEnd);
        }
        if (StrUtil.isEffective(entity.getCreateUserAccount())) {
            queryWrapper.eq("create_user_account",entity.getCreateUserAccount());
        }
        if (entity.getActive() != null) {
            queryWrapper.eq("active",entity.getActive());
        }
//        if (entity.getGrade() != null) {
//            if (entity.getGrade() == 2) {
//                queryWrapper.in("grade",2,3,4);
//            } else if (entity.getGrade() == 3) {
//                queryWrapper.in("grade",3,4);
//            } else if (entity.getGrade() == 4) {
//                queryWrapper.eq("grade",4);
//            }
//        }
        if (entity.getGrade() != null) {
            queryWrapper.eq("grade",entity.getGrade());
        }
        if (StrUtil.isEffective(entity.getBaseString())) {
            // AND (account LIKE ? OR user_name = ? OR phone = ?)
            queryWrapper.and(queryWrapper_1 ->
                    queryWrapper_1.like("account",entity.getBaseString()).or()
                            .eq("user_name",entity.getBaseString()).or()
                            .eq("phone",entity.getBaseString())
            );
        }
        return queryWrapper;
    }
}
